package com.shop.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import com.shop.dao.MemberMapper;
import com.shop.dto.MemberDto;
import com.shop.dto.MemberInfoDto;
import com.shop.dto.MemberLoginDto;
import com.shop.dto.MemberRegister;
import com.shop.entity.MemberEntity;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.time.Duration;
import java.util.List;

/**
 * @author wzby
 * @date 2022-12-02 13:24:15
 */
@Service
public class MemberService {

    @Resource(name = "redisTemplate")
    private ValueOperations<String, String> valueOperations;

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private MemberService memberService;

    String redisCode = "member.vCode.{}";

    public Integer sendVCode(@RequestParam String email) {

        MailAccount account = new MailAccount();
        account.setHost("smtp.qq.com");
        account.setPort(587);
        account.setAuth(true);
        account.setFrom("1470696181@qq.com");
        account.setUser("1470696181@qq.com");
        account.setPass("abtpvzecxtkcggib");
        String vCode = RandomUtil.randomNumbers(4);
        MailUtil.send(account, CollUtil.newArrayList(email), "验证码", "您的验证码为" + vCode, false);

        valueOperations.set(StrUtil.format(redisCode, email), vCode, Duration.ofMinutes(5));
        return 1;
    }

    public Integer register(MemberDto dto) throws Exception {
        //业务判断逻辑
        String vcode = valueOperations.get(StrUtil.format(redisCode, dto.getEmail()));
        if (ObjectUtil.isEmpty(vcode)) {
            throw new Exception("验证码过期,不存在");
        }
        if (!dto.getVcode().equals(vcode)) {
            throw new Exception("验证码不正确");
        }
        //判断手机号是否已经注册
//        if () {
//            throw new Exception("手机号已经注册");
//        }

        MemberEntity entity = BeanUtil.copyProperties(dto, MemberEntity.class);
        int ref = memberMapper.insert(entity);

        if (ref == 1) {
            MemberRegister memberRegister = new MemberRegister();
            //把这个用户注册成功的消息发到MQ
            memberRegister.setId(entity.getId());
            memberRegister.setPhone(entity.getPhone());
            memberRegister.setEmail(entity.getEmail());

            rabbitTemplate.convertAndSend("Shop-MemberRegister-wzby", "memberRegisterRouteKey", memberRegister);

            System.out.println("注册成功");
        }
        return 1;
    }

    public Integer update(MemberDto dto) {
        MemberEntity entity = BeanUtil.copyProperties(dto, MemberEntity.class);
        return memberMapper.update(entity);
    }

    public MemberInfoDto select(MemberLoginDto dto) {

        MemberEntity memberEntity = memberMapper.select(dto);

        MemberInfoDto memberInfoDto = MemberInfoDto.builder().id(memberEntity.getId()).phone(memberEntity.getPhone()).build();
        return memberInfoDto;
    }

    public Integer delete(Integer id) {
        return memberMapper.delete(id);
    }

}
